package main

import (
	"ginchat/models"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql" //这句千万别忘了
)

type Product struct {
	gorm.Model
	Code  string
	Price uint
}

func main() {
	//创建数据库连接
	db, err := gorm.Open("mysql",
		"root:root@(localhost:3306)/ginchat?charset=utf8mb4&parseTime=True&loc=Local")
	defer db.Close()

	if err != nil {
		panic("failed to connect database")
	}

	//迁移schema   这里注意是指针类型
	db.AutoMigrate(&models.UserBasic{}) //自动同步表结构

	//create
	//user := &models.UserBasic{}
	//user.Name = "申专"
	//
	////Create
	//db.Create(&user)
	//
	////Read
	//fmt.Println(db.First(&user, 1)) //根据整型主键查找
	//
	////update 将 product的price更新为 200
	//db.Model(&user).Update("PassWord", "1234")

	//Update - 更新多个字段
	//db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) //仅更新非零值字段
	//db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	//Delete - 删除 product
	//db.Delete(&product, 1)
}
